package LeetCodeP;



import java.util.*;


public class Solution01 {
    private static List<String> arrayList;

    public static int[] towSumdemo1(int[] nums,int target){
        int[] Index=new int[2];
        for(int i=0;i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if (nums[i]+nums[j]==target){
                    Index[0]=i;
                    Index[1]=j;
                }
            }
        }
        return Index;
    }


    public static int[] twoSum(int[] nums, int target) {
        int[] numsIndex = new int[2];
        for (int z=0;z<nums.length;z++){
           for (int x=z+1;x<nums.length;x++){
               if (nums[z]+nums[x]==target){
                   System.out.println(nums[z]+","+nums[x]+"="+(nums[z]+nums[x]));
                   System.out.println("["+z+","+x+"]");
                   numsIndex[0]=z;
                   numsIndex[1]=x;
                   }
               }

           }
        System.out.println(numsIndex[0]);
        System.out.println(numsIndex[1]);
        return numsIndex;
        }

     public static  int[] towSum01(int[] nums,int target){

         HashMap<Integer, Integer> map = new HashMap<>();
         for (int i=0;i<nums.length;i++){
             map.put(i,nums[i]);
         }
         for (int i=0;i<nums.length;i++){
             int complement=target-nums[i];
             System.out.println("targer:"+target+",nums:"+nums[i]+",complement："+complement);

             if (map.containsKey(complement)&&map.get(complement)!=i){

                 System.out.println("map.containsKey:"+map.containsKey(complement));
                 System.out.println("i:"+i+",map.get:"+map.get(complement));

                 return new int[]{
                         i,map.get(complement)
                 };

             }
         }
         throw new IllegalArgumentException("No two sum solution");


     }

    public static int[] twoSum02(int[] nums,int target){
        int[] index=new int[2];
        // 建立k-v ，一一对应的哈希表
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        for (int i=0;i<nums.length;i++){
            if (map.containsKey(nums[i])){
                index[0]=i;
                index[1]=map.get(nums[i]);
                System.out.println(" index[0]:"+ index[0]);
                System.out.println("index[1]:"+index[1]);
                return index;
            }
            // 将数据存入 key为补数 ，value为下标
            map.put(target-nums[i],i);

        }
        return index;
    }


    public static int[] twoSum03(int[] nums, int target) {

        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i<nums.length;i++){
            int tmp = target - nums[i];
            if(map.containsKey(tmp)){
                return new int[]{map.get(tmp),i};   //存在即返回
            }
            map.put(nums[i],i);  //不存在，则加入
        }
        throw new IllegalArgumentException("No two sum solution");
    }







    public static void main(String[] args) {
        int[] nums={2,3,4};
      /*  twoSum(nums,6);
        int[] ints = towSum01(nums, 6);

      */
   /*     twoSum02(nums,6);*/
        int[] ints = twoSum03(nums, 6);
        System.out.println(ints.toString());
        for (int i=0;i<ints.length;i++){
            System.out.println("i:"+i);
            System.out.println(ints[i]);
        }
    }
}
